from django.db import models
from common.db import DbModel
from products.models import Product
from account.models import Users
# Create your models here.


class Order(models.Model):
    """订单模型"""
    user = models.ForeignKey(Users, on_delete=models.CASCADE, verbose_name='所属用户')
    order_number = models.CharField(max_length=20, unique=True, verbose_name='订单号')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='订单总金额')
    status = models.CharField(max_length=10,
                              choices=[
                                  ('pending', '待处理'),
                                  ('processing', '处理中'),
                                  ('completed', '已完成'),
                                  ('canceled', '已取消')
                              ],
                              default='pending',
                              verbose_name='订单状态')
    pay_status = models.CharField(max_length=10,
                                  choices=[
                                      ('pending', '待支付'),
                                      ('completed', '已支付'),
                                  ],
                                  default='pending', verbose_name='支付状态'
                                )
    class Meta:
        db_table = 'orders'
        verbose_name = '订单表'
    def __str__(self):
        return self.order_number
class OrderItem(models.Model):
    """订单项模型"""
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='order_items', verbose_name='订单')
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name='商品')
    quantity = models.PositiveIntegerField(verbose_name='商品数量')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品单价')
    total_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='总价格')

    class Meta:
        db_table = 'order_items'
        verbose_name = '订单项表'
        verbose_name_plural = '订单项表'

    def __str__(self):
        return f'{self.order.order_number} - {self.product.name}'